<?php

namespace app\common\model\user;

use think\Model;

class Cash extends Model
{
    // 表名
    protected $name = 'user_cash';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'created_time_text',
        'updated_time_text',
        'deleted_time_text',
        'cash_time_text'
    ];

    public function add($data){
        if (!is_array($data)){
            renderJson('传递数据不合法', 0);
        }

        $this->allowField(true)->save($data);

        return $this->cash_id;
    }
    



    public function getCreatedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['created_time']) ? $data['created_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdatedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updated_time']) ? $data['updated_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeletedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deleted_time']) ? $data['deleted_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCashTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cash_time']) ? $data['cash_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreatedTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdatedTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setDeletedTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setCashTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function user(){
        return $this->hasOne("app\common\model\User","uid","uid");
    }


}
